{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 视频处理"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. 视频转图片"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "① cv2.VideoCapture 函数可以捕获摄像头，或读取视频文件。\n",
    "\n",
    "② cv2.VideoCapture 函数入口参数：\n",
    "\n",
    " - 用数字来控制不同的设备(摄像头)，例如 0、1。\n",
    " - 如果是视频文件，直接指定好路径即可。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2 #opencv的缩写为cv2\n",
    "import matplotlib.pyplot as plt # matplotlib库用于绘图展示\n",
    "import numpy as np   # numpy数值计算工具包\n",
    "\n",
    "# 魔法指令，直接展示图，Jupyter notebook特有\n",
    "%matplotlib inline "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "# 随着时间轴的变换，视频在动，虽然图像是静止的，但是图像在一帧一帧走，呈现视频的感觉\n",
    "\n",
    "# 绘图显示(封装函数)\n",
    "def cv_show(name,img):\n",
    "    cv2.imshow(name,img)\n",
    "    cv2.waitKey(0)\n",
    "    cv2.destroyAllWindows()\n",
    "    \n",
    "vc = cv2.VideoCapture('02_Video/00_Scenery.mp4')\n",
    "if vc.isOpened():   # 检查是否打开正确\n",
    "    open, frame = vc.read() # 这里的 vc.read() 相当于读取图像的第一帧\n",
    "                            # 若循环不断的执行 vc.read，则不断的读取第二帧、第三帧....\n",
    "    print(open) # 正常打开时，open会返回 True\n",
    "    cv_show('image_scenery',frame)\n",
    "else:\n",
    "    open = False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. 图片转视频"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "while open: # 如果正常打开，则不停循环读取,这里可替换成 i 值，来确定读取 i 帧     \n",
    "    ret, frame = vc.read()\n",
    "    if frame is None: # 视频读完以后的下一帧为空\n",
    "        break\n",
    "    if ret == True:\n",
    "        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 读取的图片转换成黑白的   \n",
    "        cv2.imshow('result',gray)\n",
    "        if cv2.waitKey(10) & 0xFF == 27: # cv2.waitKey(10)为等多少时间执行下一帧，0xFF为退出键ESC\n",
    "            break\n",
    "vc.release() # release()完成与 open() 相反的工作.释放 open() 向内核申请的所有资源\n",
    "cv2.destroyAllWindows() # 销毁所有窗口"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.6.3",
   "language": "python",
   "name": "python3.6.3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
